import {request,formatDate} from "../../utils/request"



const initDate = () => {
    const date = new Date()
    const [y, m] = [date.getFullYear(), date.getMonth()]
    const startDate = new Date(y, m,)
    const endDate = new Date(y, m + 1, 0)
    return {
        start_date: formatDate(startDate),
        end_date: formatDate(endDate)
    }
}
const {start_date, end_date} = initDate()
Page({
    data: {
        start_date,
        end_date,
        currentDate: "2024-04-01",
        active_id: wx.getStorageSync("active_id"),
        userInfo: wx.getStorageSync("userinfo"),
        historyOrder: {},
        historyMoney: {}
    },
    onLoad() {
        this.initData()
    },
    handleStartDateChange(ev) {
        const {value} = ev.detail
        const startDate = new Date(value.replace(/-/g, '/'))
        const endDate = new Date(this.data.end_date)
        if (startDate.getTime() >= endDate.getTime()) {
            return wx.showToast({
                title: "开始时间不能大于结束时间",
                icon: "none"
            })
        }
        this.setData({
            start_date: value
        })
        this.initData()
        console.log(ev, 'ev')
    },
    handleEndDateChange(ev) {
        const {value} = ev.detail
        const endDate = new Date(value.replace(/-/g, '/'))
        const startDate = new Date(this.data.start_date.replace(/-/g, '/'))
        if (endDate.getTime() <= startDate.getTime()) {
            return wx.showToast({
                title: "结束时间要大于开始时间",
                icon: "none"
            })
        }
        this.setData({
            end_date: value
        })
        this.initData()
    },
    getResult(data, keys) {
        const result = {}
        keys.forEach(k => {
            let value = data[k]
            if (!value) value = 0
            value = Number(value).toFixed(2)
            result[k] = value
        })
        return result
    },
    patchHistoryOrder(data) {
        const keys = [
            "chashi_order_num_wx",
            "chashi_order_num_yue", "chashi_order_num_meituan",
            "chashi_order_num_douyin", "shipin_order_num", "xudan_order_num", "chongzhi_num", "chayishi_num"]
        // const result = {}
        // keys.forEach(k => {
        //     let value = data[k]
        //     if (!value) value = 0
        //     value = Number(value).toFixed(2)
        //     result[k] = value
        // })
        this.setData({
            historyOrder: this.getResult(data, keys)
        })

    },
    patchHistoryMoney(data) {
        const u = {
            chashi_order_price_weixin: "",
            chashi_order_price_yue: "",
            chashi_order_price_meituan: "",
            chashi_order_price_douyin: "",
            shipin_order_price: "",
            xudan_order_price: "",
            chongzhi_price: "",
            chayishi_price: "",
            xudan_order_price_wx: "",
            xudan_order_price_yue: "",
            chashi_order_price: "",
            weixin_pay_price: ""
        }
        const keys = Object.keys(u)
        this.setData({
            historyMoney: this.getResult(data, keys)
        })
    },
    async initData() {
        wx.showLoading({
            title: "正在获取"
        })
        const {mobile} = this.data.userInfo
        const {start_date, end_date, active_id} = this.data
        const resp = await request({
            url: "/shujutongji/yuedutongji",
            data: {
                store_id: active_id,
                start_date,
                end_date,
                mobile
            }
        }).catch(() => wx.hideLoading())
        const {data} = resp
        this.patchHistoryOrder(data)
        this.patchHistoryMoney(data)
        setTimeout(() => {
            wx.hideLoading()
        }, 300)
        console.log(resp, 'resp')

    }
})
